package Client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

import Snake.Cell;

public class Grid extends JComponent {
	private static final long serialVersionUID = 1L;

	private int width;
	private int height;
	private int blockSize;
	private int specialID;
	private boolean selected = false;
	private LinkedList<Cell> occupiedCells;
	private LinkedList<Cell> barriers;
	private LinkedList<Cell> foodPowers;
	private LinkedList<Cell> jumpPowers;
	private LinkedList<Cell> boostPowers;
	private Image barrierImage, foodImage, jumpImage, boostImage;

	public Grid(int width, int height, int blockSize) {
		this.width = width;
		this.height = height;
		this.blockSize = blockSize;
		occupiedCells = new LinkedList<Cell>();
		barriers = new LinkedList<Cell>();
		foodPowers = new LinkedList<Cell>();
		jumpPowers = new LinkedList<Cell>();
		boostPowers = new LinkedList<Cell>();
		loadImages();
	}

	private void loadImages() {
		ImageIcon barrierIconImage = new ImageIcon(getClass().getResource("/images/barrier.png"));
		barrierImage = barrierIconImage.getImage();
		ImageIcon imageIconFood = new ImageIcon(getClass().getResource("/images/food.png"));
		foodImage = imageIconFood.getImage();
		ImageIcon imageIconBoost = new ImageIcon(getClass().getResource("/images/boost.png"));
		boostImage = imageIconBoost.getImage();
		ImageIcon imageIconJump = new ImageIcon(getClass().getResource("/images/jump.png"));
		jumpImage = imageIconJump.getImage();
	}

	public void fillOccupiedCells(LinkedList<Cell> ocCells) {
		for (int i = 0; i < ocCells.size(); i++) {
			occupiedCells.add(ocCells.get(i));
		}
	}

	public void fillBarriers(Cell barrierCell) {
		barriers.add(barrierCell);
	}

	public void fillFoodPowers(LinkedList<Cell> PowerCell) {
		for (int i = 0; i < PowerCell.size(); i++) {
			foodPowers.add(PowerCell.get(i));
		}
	}

	public void fillJumpPowers(LinkedList<Cell> PowerCell) {
		for (int i = 0; i < PowerCell.size(); i++) {
			jumpPowers.add(PowerCell.get(i));
		}
	}

	public void fillBoostPowers(LinkedList<Cell> PowerCell) {
		for (int i = 0; i < PowerCell.size(); i++) {
			boostPowers.add(PowerCell.get(i));
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		try {
			super.paintComponent(g);
			//synchronized (this) {
				for (int j = 0; j < height; j++) {
					g.drawLine(0, j * blockSize, width, j * blockSize);
				}
				for (int i = 0; i < width; i++) {
					g.drawLine(i * blockSize, 0, i * blockSize, height);
				}
				for (int b = 0; b < barriers.size(); b++) {
					if (barriers.get(b) != null) {
						g.drawImage(barrierImage, barriers.get(b).getX() + 2,
								barriers.get(b).getY() + 2, blockSize - 2,
								blockSize - 2, null);
					}
				}
				for (int p = 0; p < foodPowers.size(); p++) {
					if (foodPowers.get(p) != null) {
						g.drawImage(foodImage, foodPowers.get(p).getX() + 2,
								foodPowers.get(p).getY() + 2, blockSize - 2,
								blockSize - 2, null);
					}
				}
				for (int p = 0; p < jumpPowers.size(); p++) {
					if (jumpPowers.get(p) != null) {
						g.drawImage(jumpImage, jumpPowers.get(p).getX() + 2,
								jumpPowers.get(p).getY() + 2, blockSize - 2,
								blockSize - 2, null);
					}
				}
				for (int p = 0; p < boostPowers.size(); p++) {
					if (boostPowers.get(p) != null) {
						g.drawImage(boostImage, boostPowers.get(p).getX() + 2,
								boostPowers.get(p).getY() + 2, blockSize - 2,
								blockSize - 2, null);
					}
				}
				if (!occupiedCells.isEmpty()) {
					for (int a = 0; a < occupiedCells.size(); a++) {
						if (occupiedCells.get(a) != null) {
							boolean jumpB = occupiedCells.get(a).isJumpBoost();
							if (jumpB) {
								g.setColor(Color.PINK);
							}
							if (specialID == occupiedCells.get(a).getId()
									&& selected) {
								g.setColor(Color.BLUE);
							} else if (occupiedCells.get(a).getId() == 1
									&& !jumpB) {
								g.setColor(Color.GREEN);
							} else if (occupiedCells.get(a).getId() == 2
									&& !jumpB) {
								g.setColor(Color.BLACK);
							} else if (occupiedCells.get(a).getId() == 3
									&& !jumpB) {
								g.setColor(Color.ORANGE);
							} else if (occupiedCells.get(a).getId() == 4
									&& !jumpB) {
								g.setColor(Color.YELLOW);
							}

							g.drawOval(occupiedCells.get(a).getX(),
									occupiedCells.get(a).getY(), blockSize,
									blockSize);
							g.fillOval(occupiedCells.get(a).getX(),
									occupiedCells.get(a).getY(), blockSize,
									blockSize);
							if (occupiedCells.get(a).isHead()) {
								g.setColor(Color.RED);
								g.drawOval(occupiedCells.get(a).getX(),
										occupiedCells.get(a).getY(), blockSize,
										blockSize);
								g.fillOval(occupiedCells.get(a).getX(),
										occupiedCells.get(a).getY(), blockSize,
										blockSize);
							}
						}
					}
				}
			//}
		} catch (IndexOutOfBoundsException e) {
			
		} catch (NullPointerException e) {
			
		}
	}

	public int getId() {
		return specialID;
	}

	public void setID(int id) {
		this.specialID = id;
		selected = true;
	}

	public LinkedList<Cell> getOccupiedCells() {
		return occupiedCells;
	}

	public LinkedList<Cell> getFoodPowers() {
		return foodPowers;
	}

	public LinkedList<Cell> getBoostPowers() {
		return boostPowers;
	}

	public LinkedList<Cell> getJumpPowers() {
		return jumpPowers;
	}

	public void setSelected(boolean b) {
		selected = b;
	}

}
